package com.darwin.darwinUI.containers
{
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.core.UISprite;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Rectangle;
	
	/**
	 * 容器基类 所有容器都应继承此类
	 * 
	 * @author vanCopper
	 */
	public class ContainerBase extends UIComponent
	{
		protected var _container:UISprite;
		private var _bgContainer:UISprite;
		
		private var _refreshChange:Boolean = false;
		
		public function ContainerBase()
		{
			super();
			super.backgroundAuto = false;
			this.addEventListener(MouseEvent.MOUSE_WHEEL,mouseWheelHandler);
		}
		
		/**
		 * 鼠标滚轮操作处理<br/>
		 * 注意：滚轮的操作并没对内容的内边距作处理
		 */
		private function mouseWheelHandler(event:MouseEvent):void
		{
			if(mouseWheelEnabled)
			{
				var $scrollY:Number = this.scrollY - event.delta*3;
				if($scrollY<0) $scrollY = 0;
				else if($scrollY > this.getMaxScrollY() - this.height) $scrollY = this.getMaxScrollY() - this.height;
				
				this.scrollY = $scrollY;
			}
		}
		
		private var _mouseWheelEnabled:Boolean = false;

		/**
		 * 是否启用滚轮滚动内容
		 */
		public function get mouseWheelEnabled():Boolean
		{
			return _mouseWheelEnabled;
		}

		public function set mouseWheelEnabled(value:Boolean):void
		{
			_mouseWheelEnabled = value;
		}
		
		
		
		
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			updateLayout();
			refresh();
		}
		/**
		 * 初始化组件 
		 * 
		 */		
		override protected function initUIComponent():void
		{
			super.initUIComponent();
			_container = new UISprite();
			super.addChild(_container);
			_bgContainer = new UISprite();
			_bgContainer.mouseEnabled = false;
			_bgContainer.mouseChildren = false;
			super.addChildAt(_bgContainer,0);
			refresh();
		}
		
		/**
		 * 向容器添加子项 
		 * @param item
		 * 
		 */		
		public function addItem(item:UIComponent):void
		{
			_container.addChild(item);
			if(this.stage)this.stage.invalidate();
		}
	
		/**
		 * 向容器的指定索引处 添加子项
		 * @param item
		 * @param index
		 * 
		 */		
		public function addItemAt(item:UIComponent,index:int):void
		{
			_container.addChildAt(item,index);
			if(this.stage)this.stage.invalidate();
		}
		
		/**
		 * 移除指定子项 
		 * 
		 */		
		public function removeItem(item:UIComponent):void
		{
			_container.removeChild(item);
			if(this.stage)this.stage.invalidate();
		}
		
		/**
		 * 移除指定索引对应子项 
		 * @param index
		 * 
		 */		
		public function removeItemAt(index:int):UIComponent
		{
			
			var item:UIComponent;
			if(_container.numChildren > 0)
			{
				item = _container.removeChildAt(index) as UIComponent;
				if(this.stage)this.stage.invalidate();
			}
			return item;
		}
		
		/**
		 * 根据索引交换两个子项 
		 * @param index1
		 * @param index2
		 * 
		 */		
		public function swapItemsAt(index1:int,index2:int):void
		{
			if(index1>=0 && index2>=0 && index1<_container.numChildren && index2<_container.numChildren)
			{
				_container.swapChildrenAt(index1,index2);
				if(this.stage)this.stage.invalidate();
			}
		}
		
		/**
		 * 获取指定索引的子项 
		 * @param index
		 * @return 
		 * 
		 */		
		public function getItemAt(index:int):UIComponent
		{
			return _container.getChildAt(index) as UIComponent;
		}
		
		/**
		 * 获取容器子项个数 
		 * @return 
		 * 
		 */		
		public function get numItems():int
		{
			return _container.numChildren;
		}
		
		/**
		 * 移除容器所有子项 
		 * 
		 */		
		public function removeAllItem():void
		{
			_container.removeAllChild();
			if(this.stage)this.stage.invalidate();
		}
		
		/**
		 * 更新容器布局 
		 * 
		 */		
		protected function updateLayout():void
		{
			_container.x = leftDistance;
			_container.y = topDistance;
		}
		
		/**
		 * 调用addItem()添加子项 
		 * @param child
		 * @return 
		 * 
		 */		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			throw new Error("添加到容器的子项必须为UIComponent并调用 addItem()方法添加");
			return child;
		}
		
		/**
		 * 调用addItemAt()添加子项 
		 * @param child
		 * @param index
		 * @return 
		 * 
		 */		
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			throw new Error("添加到容器的子项必须为UIComponent并调用 addItemAt()方法添加");
			return child;
		}
		
		/**
		 * 调用 removeItem(); 
		 * @param child
		 * @return 
		 * 
		 */		
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			throw new Error("调用 removeItem()方法移除子项");
			return child;
		}
		
		/**
		 * 调用removeItemAt()方法
		 * @param index
		 * @return 
		 * 
		 */		
		override public function removeChildAt(index:int):DisplayObject
		{
			throw new Error("调用 removeItemAt()方法移除子项");
		}
		
		/**
		 * 调用 removeAllItem()方法移除所有项 
		 * 
		 */		
		override public function removeAllChild():void
		{
			throw new Error("调用 removeAllItem()方法移除所有子项");
		}
		
		private var _leftDistance:Number = 0;
		private var _topDistance:Number = 0;
		private var _rightDistance:Number = 0;
		private var _bottomDistance:Number = 0;
		
		/**
		 * 所有子项的左边距 
		 */
		public function get leftDistance():Number
		{
			return _leftDistance;
		}
		
		/**
		 * @private
		 */
		public function set leftDistance(value:Number):void
		{
			_leftDistance = value;
			_refreshChange = true;
			if(this.stage) this.stage.invalidate();
		}
		
		/**
		 * 所有子项的上边距 
		 */
		public function get topDistance():Number
		{
			return _topDistance;
		}
		
		/**
		 * @private
		 */
		public function set topDistance(value:Number):void
		{
			_topDistance = value;
			_refreshChange = true;
			if(this.stage)this.stage.invalidate();
		}

		
		private var _scrollRect:Rectangle;
		
		override public function set scrollRect(value:Rectangle):void
		{
			
//			_scrollRect = value;	
//			sizeChange = true;
//			if(this.stage)this.stage.invalidate();
		}
		
		override public function get scrollRect():Rectangle
		{
			return null;	
		}
		
		/**
		 * 容器类 无法设置自适应背景 
		 * @param value
		 * 
		 */		
		override public function set backgroundAuto(value:Boolean):void
		{
//			super.backgroundAuto = value;
		}
		
		protected var _scrollX:Number = 0;
		protected var _scrollY:Number = 0;
		
		override protected function refresh():void
		{
			if(autoSize)
			{
				var mw:Number = getMaxScrollX();
				var mh:Number = getMaxScrollY();
				
				width = mw + leftDistance + rightDistance;
				height = mh + topDistance + bottomDistance;
			}
			
			if(sizeChange || _refreshChange)
			{
				_refreshChange = false;
				var rw:Number = width  - leftDistance - rightDistance;
				var rh:Number = height - topDistance - bottomDistance;
				if(_scrollRect==null)
				{
					_scrollRect = new Rectangle(scrollX,scrollY,rw,rh);
				}else
				{
					_scrollRect.x = scrollX;
					_scrollRect.y = scrollY;
					_scrollRect.width = rw;
					_scrollRect.height = rh;
				}
				_container.scrollRect = _scrollRect;
			}
			
			super.refresh();
		}
		
		/**
		 * 获取x轴 最大可滚动数值 
		 * @return 
		 * 
		 */		
		public function getMaxScrollX():Number
		{
			var num:int = 0;
			var maxW:Number = 0;
			var preW:Number = 0;
			while(num < _container.numChildren)
			{
				var child:UIComponent = _container.getChildAt(num) as UIComponent;
				num++;
				var tempW:Number = child.x + child.width;
				maxW = Math.max(tempW,preW);
				preW = maxW;
			}
			return maxW;
		}
		
		/**
		 * 获取Y轴 最大可滚动数值 
		 * @return 
		 * 
		 */		
		public function getMaxScrollY():Number
		{
			var num:int = 0;
			var maxH:Number = 0;
			var preH:Number = 0;
			while(num < _container.numChildren)
			{
				var child:UIComponent = _container.getChildAt(num) as UIComponent;
				num++;
				var tempW:Number = child.y + child.height;
				maxH = Math.max(tempW,preH);
				preH = maxH;
			}
			return maxH;
		}
		
		
		/**
		 * 取得内容的宽度
		 */
		public function get contentWidth():Number
		{
			return getMaxScrollX();
		}
		
		/**
		 * 取得内容高度
		 */
		public function get contentHeight():Number
		{
			return getMaxScrollY();
		}
		
		

		/**
		 * 改变可视区域x值 
		 */
		public function get scrollX():Number
		{
			return _scrollX;
		}

		/**
		 * @private
		 */
		public function set scrollX(value:Number):void
		{
			_scrollX = value;
			_refreshChange = true;
			if(this.stage)this.stage.invalidate();
		}

		/**
		 * 改变可视区域的Y值 
		 */
		public function get scrollY():Number
		{
			return _scrollY;
		}

		/**
		 * @private
		 */
		public function set scrollY(value:Number):void
		{
			_scrollY = value;
			_refreshChange = true;
			if(this.stage)this.stage.invalidate();
		}

		/**
		 * 所有子项的右边距 
		 */
		public function get rightDistance():Number
		{
			return _rightDistance;
		}

		/**
		 * @private
		 */
		public function set rightDistance(value:Number):void
		{
			_rightDistance = value;
			_refreshChange = true;
			if(this.stage)this.stage.invalidate();
		}

		/**
		 * 所有子项的底边距 
		 */
		public function get bottomDistance():Number
		{
			return _bottomDistance;
		}

		/**
		 * @private
		 */
		public function set bottomDistance(value:Number):void
		{
			_bottomDistance = value;
			_refreshChange = true;
			if(this.stage)this.stage.invalidate();
		}

		/**
		 * 子项列表 
		 */
		public function get itemList():Vector.<UIComponent>
		{
			var num:int = 0;
			var _itemList:Vector.<UIComponent> = new Vector.<UIComponent>();
			while(num < _container.numChildren)
			{
				var child:UIComponent = _container.getChildAt(num) as UIComponent;
				_itemList.push(child);
				num ++;
			}
			return _itemList.concat();
		}
		
		override public function set background(value:DisplayObject):void
		{
			if(_background && _background.parent)
			{
				_bgContainer.removeChild(_background);
			}
			
			_background = value;
			if(_background != null)
			{
				_bgContainer.addChild(_background);
			}
			_backgroundChange = true;
			invalidate();
		}
		
		override public function set width(value:Number):void
		{
			super.width = value;
		}
		
		override protected function initDefaultSkin():void
		{
		}
	}
}